-- PixyWell
-- Created by chengb Feb/3/2016
-- 妖精之井格子

-- 初始化
function initPixyWell(self)
    self.item:removeAllChildren();

    local dbase = DungeonM.queryElement(self.gridData.element, "dbase");
    if type(dbase) == "table" and type(dbase["need_property"]) == "number" then
        -- 配置了需要道具才出现
        if ItemM.getAmount(ME.user, dbase["need_property"]) <= 0 then
            -- 包裹中没有需求的道具
            -- 走另外的初始化流程
            initPixyWell2(self);
            return;
        end
    end

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 点击"一枚金币"
    local function okFunc()
        local tipList = splitString(getLocStr("one_coin_tip"), "\n");
        splitAlert(tipList);
    end

    -- 点击"妖精的钱币"
    local function cancelFunc()
        local tipList = splitString(getLocStr("pixy_well_coin_tip"), "\n");
        splitAlert(tipList);

        -- 执行奖励
        local pos = self.gridData:getPos();
        DungeonActionM.go("pixy_well_event", pos);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 打开建筑查看界面2
            self:openBuildingView2(okFunc, cancelFunc);

            -- 音效
            AudioM.playFx("ui_open1");
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 是否闪光
    if buildingInfo["shinning"] == 1 then
        self.shiningAjustPos = cc.p(70, 100);
        shiningEffect(self.icon, self.shiningAjustPos.x, self.shiningAjustPos.y);
    end
end

-- 妖精之井2（没有道具"妖精的钱币"时才显示）
function initPixyWell2(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local pos = self.gridData:getPos();

    -- 点击"投50金币"
    local function okFunc()
        DungeonActionM.go("pixy_throw_coin", pos, 1);
    end

    -- 点击"投100金币"
    local function cancelFunc()
        DungeonActionM.go("pixy_throw_coin", pos, 2);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 打开建筑查看界面2
            local arr = FormulaM.invoke("CALC_PIXY_WELL_COIN_OPTIONS");
            self:openBuildingView2(okFunc, cancelFunc,
                string.format(getLocStr("btn_text_throw_coin"), arr[1]),
                string.format(getLocStr("btn_text_throw_coin"), arr[2]));

            -- 音效
            AudioM.playFx("ui_open1");
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 是否闪光
    if buildingInfo["shinning"] == 1 then
        self.shiningAjustPos = cc.p(70, 100);
        shiningEffect(self.icon, self.shiningAjustPos.x, self.shiningAjustPos.y);
    end
end

-- 往妖精之井中投放金币的事件
function onThrowCoinToWell(self, bonus, optionIndex)
    local arr = FormulaM.invoke("CALC_PIXY_WELL_COIN_OPTIONS");
    local amount = arr[optionIndex];
    local msg = string.format(getLocStr("throw_coin_to_well_tip"), amount);
    local tipList = splitString(msg, "\n");
    splitAlert(tipList);

    self:playBonusListEffect(bonus);
end
